package cn.com.surker.oa.service.impl;

import cn.com.surker.common.Code;
import cn.com.surker.oa.dao.OaSupplierAccountDao;
import cn.com.surker.oa.dao.OaSupplierOrgDao;
import cn.com.surker.oa.entity.OaSupplierAccount;
import cn.com.surker.oa.entity.OaSupplierOrg;
import cn.com.surker.oa.service.OaSupplierAccountService;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* <p>
    * 供应商收款账户 服务实现类
    * </p>
*
* @author admin
* @since 2022-10-05
*/
@Service
public class OaSupplierAccountServiceImpl extends ServiceImpl<OaSupplierAccountDao, OaSupplierAccount> implements OaSupplierAccountService {

    @Resource
    private OaSupplierAccountDao supplierAccountDao;
    @Resource
    private OaSupplierOrgDao supplierOrgDao;

    /**
     * 根据公司查询付款账户
     * @param sid 供应商ID
     * @param orgId 公司ID
     * @return
     */
    public List<OaSupplierAccount> queryByOrg(Long sid, Long orgId, Long uid) {
        if (StringUtils.isAllParamNull(sid))
            return null;
        QueryWrapper<OaSupplierAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sid", sid);
        if (!StringUtils.isNull(orgId))
            queryWrapper.eq("org_id", orgId);
        if (!StringUtils.isNull(uid))
            queryWrapper.eq("uid", uid);
        queryWrapper.eq("status", 0);
        List<OaSupplierAccount> list = supplierAccountDao.selectList(queryWrapper);
        return list;
    }

    /**
     * 根据公司查询付款账户
     * @param sid 供应商ID
     * @return
     */
    public List<OaSupplierAccount> queryByOrgV1(Long sid, Long uid) {
        if (StringUtils.isAllParamNull(sid))
            return null;
        QueryWrapper<OaSupplierAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sid", sid);
        if (!StringUtils.isNull(uid))
            queryWrapper.inSql("org_id", "select pid from sys_origin where status = 0 and type = 1 " +
                    " and id in (select org_id from sys_user_position where uid = " + uid + " and status = 0) group by pid");
        queryWrapper.eq("status", 0);
        List<OaSupplierAccount> list = supplierAccountDao.selectList(queryWrapper);

        return list;
    }

    /**
     * 分页查询账户信息
     * @param searchVal 查询条件
     * @param status 状态
     * @param offset
     * @param limit
     * @return
     */
    public Map<String, Object> queryPage(String searchVal, Long orgId, Integer status, Integer offset, Integer limit) {
        if (StringUtils.isNull(searchVal))
            searchVal = "";
        if (StringUtils.isNull(offset))
            offset = 0;
        if (StringUtils.isNull(limit))
            limit = 15;

        List<Map<String, Object>> list = supplierAccountDao.queryByPage(searchVal, orgId, status, offset, limit);
        Integer total = supplierAccountDao.queryByTotal(searchVal, orgId, status);
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("data", list);

        return map;
    }

    /**
     * 新增供应商账户
     * @param realName 收款名称
     * @param bankName 开户行
     * @param bankNo 银行账户
     * @param sid 供应商ID
     * @param orgId 公司ID
     * @param orgName 公司名称
     * @param uid 创建人id
     * @param uid 创建人
     * @param remark 备注
     * @return
     */
    public Integer save(String realName, String bankName, String bankNo, Long sid, Long orgId, Long uid,
                 String creator, String orgName, String remark) {
        if (StringUtils.isNull(realName))
            return Code.NO_ACCOUNT_REALNAME_EMPTY.getCode();
        if (StringUtils.isNull(bankName))
            return Code.NO_ACCOUNT_BANK_NAME_EMPTY.getCode();
        if (StringUtils.isNull(bankNo))
            return Code.NO_ACCOUNT_BANK_EMPTY.getCode();
        if (StringUtils.isNull(sid))
            return Code.NO_SUPPLIER_NOT.getCode();
        if (StringUtils.isAllParamNull(uid, creator))
            return Code.UN_USER_INFO.getCode();
        if (StringUtils.isAllParamNull(orgId, orgName))
            return Code.NO_SUPPLIER_ORIGIN_EMPTY.getCode();
        bankNo = bankNo.replace(" ", "");
        bankName = bankName.replaceAll(" ", "").replaceAll("（", "(").replaceAll("）", ")");

        QueryWrapper<OaSupplierOrg> query = new QueryWrapper<>();
        query.eq("supplier_id", sid);
        query.eq("status", 0);
        query.eq("org_id", orgId);
        query.groupBy("org_id", "org_name");
        List<OaSupplierOrg> orgList = supplierOrgDao.selectList(query);
        if (orgList != null && orgList.size() > 0) {
            for (int i = 0; i < orgList.size(); i++) {
                OaSupplierOrg org = orgList.get(i);
                QueryWrapper<OaSupplierAccount> accountQuery = new QueryWrapper<>();
                accountQuery.eq("bank_no", bankNo);
                accountQuery.eq("org_id", org.getOrgId());
                accountQuery.eq("sid", sid);
                accountQuery.eq("status", 0);
                Integer total = supplierAccountDao.selectCount(accountQuery);
                if (total > 0)
                    continue;
                OaSupplierAccount supplierAccount = new OaSupplierAccount(realName, bankName, bankNo, sid, org.getOrgId(), uid,
                        creator, 0, remark, new Date(), org.getOrgName());
                Integer resultCode = supplierAccountDao.insert(supplierAccount);
            }
            return 0;
        } else {
            OaSupplierAccount supplierAccount = new OaSupplierAccount(realName, bankName, bankNo, sid, orgId, uid,
                    creator, 0, remark, new Date(), orgName);
            Integer resultCode = supplierAccountDao.insert(supplierAccount);
            if (resultCode > 0)
                return 0;
        }

        return -1;
    }

    /**
     * 更新供应商账户
     * @param id 标识
     * @param realName 收款名称
     * @param bankName 开户行
     * @param bankNo 银行账户
     * @param sid 供应商ID
     * @param orgId 公司ID
     * @param orgName 公司名称
     * @param uid 创建人id
     * @param uid 创建人
     * @param remark 备注
     * @param status 状态(0：启用，1：禁用)
     * @return
     */
    public Integer update(Long id, String realName, String bankName, String bankNo, Long sid, Long orgId, Long uid,
                   String creator, String orgName, String remark, Integer status) {
        if (StringUtils.isNull(id))
            return Code.NO_ACCOUNT_EMPTY.getCode();

        OaSupplierAccount supplierAccount = supplierAccountDao.selectById(id);
        if (supplierAccount == null)
            return Code.NO_ACCOUNT_EMPTY.getCode();
        if (!StringUtils.isNull(realName))
            supplierAccount.setRealName(realName);
        if (!StringUtils.isNull(bankName)) {
            bankName = bankName.replaceAll(" ", "").replaceAll("（", "(").replaceAll("）", ")");
            supplierAccount.setBankName(bankName);
        }
        if (!StringUtils.isNull(bankNo)) {
            bankNo = bankNo.replace(" ", "");
            supplierAccount.setBankNo(bankNo);
        }
        if (!StringUtils.isNull(sid))
            supplierAccount.setSid(sid);
        if (!StringUtils.isNull(orgId))
            supplierAccount.setOrgId(orgId);
        if (!StringUtils.isNull(uid))
            supplierAccount.setUid(uid);
        if (!StringUtils.isNull(creator))
            supplierAccount.setCreator(creator);
        if (!StringUtils.isNull(orgName))
            supplierAccount.setOrgName(orgName);
        if (!StringUtils.isNull(remark))
            supplierAccount.setRemark(remark);
        if (!StringUtils.isNull(status))
            supplierAccount.setStatus(status);

        supplierAccount.setCreateTime(new Date());
        Integer resultCode = supplierAccountDao.updateById(supplierAccount);
        if (resultCode > 0)
            return 0;
        return -1;
    }

}